//
//  ConvenientPayConfirmEngine.m
//  HuLaQuan
//
//  Created by hok on 2/5/16.
//  Copyright © 2016 yuwubao. All rights reserved.
//

#import "ConvenientPayConfirmEngine.h"

#import "CurrentUser.h"

@implementation ConvenientPayConfirmEngine

- (void)getPaymentInfo:(NSDictionary *)card andOrderInfo:(NSDictionary *)orderInfo 
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getPayInfo\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\" ><orderId>%@</orderId><discountCode>%@</discountCode><payType>%@</payType><accType>%@</accType><cardId>%@</cardId><txnAmt>%@</txnAmt><payAmt>%@</payAmt><appCode>%@</appCode><resType>%@</resType><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID ,orderInfo[@"orderID"],@"0000",kPayTypeOfConvenientPay,card[@"accType"],card[@"cardId"],orderInfo[@"txnAmt"],orderInfo[@"payAmt"],orderInfo[@"appCode"],orderInfo[@"resType"]];
    NSLog(@"%@ -- src --- ",src);
    
    
    self.orderInfo = orderInfo;
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CASHIER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            self.paymentInfo = resultDic;
            
            NSLog(@"%@",resultDic);
            
            result = @{@"isSuccess":@YES};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetConvenientPaymentInfoFinish" object:result];
    }];

    
}
- (void)payScanOrder:(NSDictionary *)passwordInfo withCard:(NSDictionary *)card
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"payOrderA\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@|%@\" userId=\"%@\" channelId=\"88888888\" mchntId=\"443701060120050\" ><orderId>%@</orderId><resType>%@</resType><resId>%@</resId><resName>%@</resName><payMode>0</payMode><payType>%@</payType><txnAmt>%@</txnAmt><payAmt>%@</payAmt><cardId>%@</cardId><appCode>%@</appCode><notifySysName>%@</notifySysName><noticeSysUrl>%@</noticeSysUrl><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,passwordInfo[@"tokenValue"],[CurrentUser sharedInstance].ID ,self.orderInfo[@"orderId"],self.orderInfo[@"resType"],self.orderInfo[@"resId"],self.orderInfo[@"resName"],kPayTypeOfConvenientPay,self.paymentInfo[@"txnAmt"],self.paymentInfo[@"payAmt"],card[@"cardId"],self.orderInfo[@"appCode"],self.orderInfo[@"notifySysName"],self.orderInfo[@"notifySysUrl"]];
    
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CASHIER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            
            
            NSLog(@"%@",resultDic);
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PayConvenientOrderFinish" object:result];
    }];
    
}

- (void)payConvenientOrderwithCard:(NSDictionary *)card
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"payOrderA\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\" mchntId=\"443701060120050\" ><orderId>%@</orderId><resType>%@</resType><resId>%@</resId><resName>%@</resName><payMode>0</payMode><payType>%@</payType><txnAmt>%@</txnAmt><payAmt>%@</payAmt><cardId>%@</cardId><appCode>%@</appCode><notifySysName>%@</notifySysName><noticeSysUrl>%@</noticeSysUrl><attach>%@</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID ,self.orderInfo[@"orderId"],self.orderInfo[@"resType"],self.orderInfo[@"resId"],self.orderInfo[@"resName"],kPayTypeOfConvenientPay,self.paymentInfo[@"txnAmt"],self.paymentInfo[@"payAmt"],card[@"cardId"],self.orderInfo[@"appCode"],self.orderInfo[@"notifySysName"],self.orderInfo[@"notifySysUrl"],@{@"payLimit":@"true"}];
    
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CASHIER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            
            
            NSLog(@"%@",resultDic);
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PayConvenientOrderFinish" object:result];
    }];
    
}
@end
